#!/usr/bin/python

'''
ZetCode PyCairo tutorial

This program creates a 'puff'
effect.

author: Jan Bodnar
website: zetcode.com
last edited: August 2012
'''


from gi.repository import Gtk, GLib, Gdk
import cairo


class cv(object):

    SPEED = 14
    TEXT_SIZE_MIN = 10
    TEXT_SIZE_MAX = 20
    FONT_ALPHA_STEP = 0.01
    FONT_SIZE_STEP = 0.5
    SIGN = 1


class Example(Gtk.Window):

    def __init__(self):
        super(Example, self).__init__()

        self.init_ui()

    def init_ui(self):
        self.darea = Gtk.DrawingArea()
        self.darea.connect("draw", self.on_draw)
        self.add(self.darea)

        self.timer = True
        self.alpha = 1.0
        self.size = 1.0

        GLib.timeout_add(cv.SPEED, self.on_timer)

        self.set_title("Puff")
        self.resize(350, 200)
        self.set_position(Gtk.WindowPosition.CENTER)
        self.connect("delete-event", Gtk.main_quit)
        # self.get_window().set_decorations(Gdk.WMDecoration.BORDER)
        self.show_all()
        # self.set_decorated(False)
        # self.set_titlebar(label)

    def on_timer(self):
        if not self.timer: return False

        self.darea.queue_draw()
        return True

    def on_draw(self, wid, cr):
        w, h = self.get_size()

        cr.set_source_rgba(0.5, 0, 0, 0.9)
        cr.paint()
        cr.select_font_face("Courier", cairo.FONT_SLANT_NORMAL,
                            cairo.FONT_WEIGHT_BOLD)

        self.size += (cv.FONT_SIZE_STEP * cv.SIGN)
        if self.size > cv.TEXT_SIZE_MAX: self.alpha -= (cv.FONT_ALPHA_STEP * cv.SIGN)

        if self.size < cv.TEXT_SIZE_MIN: cv.SIGN = 1
        if self.alpha < 0.1: cv.SIGN = -1

        cr.set_font_size(self.size)
        cr.set_source_rgb(1, 1, 1)

        (x, y, width, height, dx, dy) = cr.text_extents("ZetCode")

        cr.move_to(w/2 - width/2, h/2)
        cr.text_path("ZetCode")
        cr.clip()
        cr.paint_with_alpha(self.alpha)

        # if self.alpha <= 0:
        #     self.timer = False


def main():
    app = Example()
    Gtk.main()


if __name__ == "__main__":
    main()